define(['../../carrys/cityList','../../carrys/jsonList','../../../../js/lib/other/sweet-alert'],function(cityData,jsonList) {
    return ['$scope','$location', '$interval','$timeout','bizProxy', function($scope,$location,$interval,$timeout,bizProxy) {
    	$scope.$emit("top-show",'true');//顶部显示
    	$scope.$emit('menuTypeLight','loan'); 	//控制底部menu高亮
    	$scope.$emit('bub-menu','true'); 	//隐藏底部menu
    	$scope.$emit('flag-tag','57'); 	//显示底部文字
    	
    	
    	//整理省市数据
    	var provinceList = new Array();
    	var cityListTemp = new Array();
    	var districtTemp = new Array();
    	collateData();//整理列表数据
    	
    	//申请人姓名
    	if(resultData.loginUserData.userName != "" && resultData.loginUserData.userName != null){
    		$scope.applyName = resultData.loginUserData.userName;
		}else{
			$scope.applyName = "";
		}
    	
    	//身份证号
    	if(resultData.loginUserData.idNo != "" && resultData.loginUserData.idNo != null){
    		$scope.idCardNo = resultData.loginUserData.idNo;
		}else{
			$scope.idCardNo = "";
		}
    	
    	//执业证号
    	if(resultData.loginUserData.lawyerNo != "" && resultData.loginUserData.lawyerNo != null){
    		$scope.practiceNo = resultData.loginUserData.lawyerNo;
		}else{
			$scope.practiceNo = "";
		}
    	
    	$scope.lawyerAreaName = "";//律所名称
    	
    	//联系方式
    	if(resultData.loginUserData.mobileNo != "" && resultData.loginUserData.mobileNo != null){
    		$scope.telNo = resultData.loginUserData.mobileNo;
		}else{
			$scope.telNo = "";
		}
    	
    	//联系地址
    	$scope.provinceName = "",$scope.cityName ="",$scope.districtName = "";$scope.detailAddr = "";
    	
    	//原告
    	$scope.prosecutor = "";
    	
    	//被告
    	$scope.accused = "";
    	
    	//案由
    	$scope.causeActionList = [{'name':'合同纠纷','val':'1'},
	    	  			            {'name':'股权纠纷','val':'2'},
	    				            {'name':'侵权赔偿案件','val':'3'},
	    				            {'name':'人身损害及其他','val':'4'}
    		                    ]
    	$scope.describeCase = "";
    	
    	//案情描述
//    	$scope.describeCase = "";
    	
    	//案件进展情况
    	$scope.caseProgress = "";
    	
    	//索赔金额
    	$scope.claimAmt = "";
    	
    	//以往索赔
    	$scope.oldClaimAmt = "";
    	
    	//本案预计获赔
    	$scope.thisClaimAmt = "";
    	
    	//案件所在地
    	$scope.provinceName1 = "",$scope.cityName1 ="",$scope.districtName1 = "";
    	
    	//管辖法院
    	$scope.obedientLawer = "";
    	
    	//拟结案期限
    	$scope.closeCaseList = [{'name':'1-6个月','val':'1'},
    	  			            {'name':'6-9个月','val':'2'},
    				            {'name':'9-12个月','val':'3'},
    				            {'name':'12-15个月','val':'4'},
    				            {'name':'15个月以上','val':'5'}
		                    ];
    	$scope.closeDate = "";
    	
    	//需投资总金额
    	$scope.investAllAmt = "";
    	
    	//预计放款时间
    	$scope.extendLoanDate = '';
    	
    	//收款人帐号
    	if(resultData.loginUserData.accountNo != "" && resultData.loginUserData.accountNo != null){
    		$scope.bankCardNo = resultData.loginUserData.accountNo;
		}else{
			$scope.bankCardNo = "";
		}
    	
    	//银行支行
    	if(resultData.loginUserData.accountNo != "" && resultData.loginUserData.accountNo != null){
    		$scope.branchName = resultData.loginUserData.branchName;
		}else{
			$scope.branchName = "";
		}
    	
    	//账户名称
    	if(resultData.loginUserData.accountNo != "" && resultData.loginUserData.accountNo != null){
    		$scope.accountName =resultData.loginUserData.accountName;
		}else{
			$scope.accountName = "";
		}
    	
    	
    	//收款银行名称
    	if(resultData.loginUserData.bankName != "" && resultData.loginUserData.bankName != null){
    		$scope.bankCardName = resultData.loginUserData.bankName;
		}else{
			$scope.bankCardName = "";
		}
    	
    	
    	
//    	bizInfo.courtProvince = ($scope.provinceName != "" && $scope.provinceName != null)?($scope.provinceName).province : "";
//    	bizInfo.courtCity = ($scope.cityName != "" && $scope.cityName != null)?($scope.cityName).city : "";
//    	bizInfo.courtCounty = ($scope.districtName != "" && $scope.districtName != null)?($scope.districtName).district : "";
    	
    	//显示省份
    	$scope.provinceData = provinceList;
    	//显示城市
    	$scope.clickCity = function(data,flag){
    		var cityList = new Array();
    		var provinceName = data.province;
    		for(var i=0;i<cityListTemp.length;i++){
    			if(cityListTemp[i].province == provinceName){
    				var tempData = {'city': cityListTemp[i].city};
    				cityList.push(tempData);
    			}
    		}
    		if(flag === '0'){
    			$scope.cityData = cityList;
    		}else if(flag === '1'){
    			$scope.cityData1 = cityList;
    		}else if(flag === '2'){
//    			$scope.cityData2 = cityList;
    		}
    		
    	}
    	//显示地区
    	$scope.clickArea = function(data,flag) {
    		var districtList = new Array();
    		var districtList1 = new Array();
    		var cityName = data.city;
    		for(var i=0;i<districtTemp.length;i++){
    			if(districtTemp[i].city == cityName){
    				districtList = districtTemp[i].district;
    				break;
    			}
    		}
    		for(var i=0;i< districtList.length;i++){
    			var tempData = {'district':districtList[i]};
    			districtList1.push(tempData);
    		}
    		if(flag === '0'){
    			$scope.districtData = districtList1;
    		}else if(flag === '1'){
    			$scope.districtData1 = districtList1;
    		}else if(flag === '2'){
//    			$scope.districtData2 = districtList1;
    		}
    		
    	}
    	
    	//点击提交信息
    	$scope.goNext = function(){
    		if(! $scope.applyName){
    			$scope.$emit("promptAlertMsg","申请人不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}
//    		else if(! $scope.idCardNo){
//    			$scope.$emit("promptAlertMsg","身份证号不能为空");
//    			$scope.$emit("showCollect",true);
//    			$timeout(function(){
//    				$scope.$emit("showCollect",false);
//    			},transData.promptTime);
//                return;
//    		}
    		else if(! $scope.practiceNo){
    			$scope.$emit("promptAlertMsg","律师执业证号不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}else if(isNaN($scope.practiceNo *1)){
    			$scope.$emit("promptAlertMsg","律师执业证号格式不正确");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}else if(! $scope.lawyerAreaName){
    			$scope.$emit("promptAlertMsg","律所名称不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}else if(! _SYT_util.isValidNo($scope.telNo)){
    			$scope.$emit("promptAlertMsg","手机号格式不正确");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}else if(! ($scope.provinceName).province){
    			$scope.$emit("promptAlertMsg","联系地址省不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}else if(! ($scope.cityName).city){
    			$scope.$emit("promptAlertMsg","联系地址市不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}else if(! ($scope.districtName).district){
    			$scope.$emit("promptAlertMsg","联系地址区\/县不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}else if(! $scope.detailAddr){
    			$scope.$emit("promptAlertMsg","联系地址详细不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}else if(! $scope.prosecutor){
    			$scope.$emit("promptAlertMsg","原告不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}else if(! $scope.accused){
    			$scope.$emit("promptAlertMsg","被告不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}else if(! $scope.describeCase){
    			$scope.$emit("promptAlertMsg","案由不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}else if(! $scope.caseProgress){
    			$scope.$emit("promptAlertMsg","案件进展不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}else if(! $scope.claimAmt){
    			$scope.$emit("promptAlertMsg","索赔金额格式不正确");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}else if(! $scope.oldClaimAmt){
    			$scope.$emit("promptAlertMsg","以往同类案件索赔格式不正确");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}else if(! $scope.thisClaimAmt){
    			$scope.$emit("promptAlertMsg","本案预计索赔格式不正确");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}else if(! ($scope.provinceName1).province){
    			$scope.$emit("promptAlertMsg","管辖法院省不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}else if(! ($scope.cityName1).city){
    			$scope.$emit("promptAlertMsg","管辖法院市不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}else if(! ($scope.districtName1).district){
    			$scope.$emit("promptAlertMsg","管辖法院区\/县不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}else if(! $scope.obedientLawer){
    			$scope.$emit("promptAlertMsg","管辖法院称不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}else if(! $scope.closeDate){
    			$scope.$emit("promptAlertMsg","拟结案期限不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}else if(! $scope.investAllAmt){
    			$scope.$emit("promptAlertMsg","需投资总金额格式不正确");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}else if(! $scope.extendLoanDate){
    			$scope.$emit("promptAlertMsg","期望放款日期不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}else if(! $scope.bankCardName){
    			$scope.$emit("promptAlertMsg","银行名称不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}else if(! $scope.branchName){
    			$scope.$emit("promptAlertMsg","银行支行名称不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}else if(! $scope.accountName){
    			$scope.$emit("promptAlertMsg","账户名称不能为空");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}else if(! _YHC_util.luhmCheck($scope.bankCardNo)){
    			$scope.$emit("promptAlertMsg","银行账号格式不正确");
    			$scope.$emit("showCollect",true);
    			$timeout(function(){
    				$scope.$emit("showCollect",false);
    			},transData.promptTime);
                return;
    		}
    		subApplyData();
    	}
    	
    	 //提交申请资料
        function subApplyData(){
        	var headerInfo = {},bizInfo = {};
        	headerInfo.pageNum = '';
        	headerInfo.pageSize = '';
        	headerInfo.sessionId = transData.sessionId;
        	
        	bizInfo.inviteCode = resultData.loginUserData.inviteCode;
        	bizInfo.lawyerNo = $scope.practiceNo;
        	bizInfo.applyUserName = $scope.applyName;
        	bizInfo.lawyerCompName = $scope.lawyerAreaName;
        	bizInfo.mobileNo = $scope.telNo;
        	bizInfo.province = ($scope.provinceName).province;
        	bizInfo.city = ($scope.cityName).city;
        	bizInfo.county = ($scope.districtName).district;
        	bizInfo.address = $scope.detailAddr;
        	bizInfo.creditorName = $scope.prosecutor; 
        	bizInfo.debtorName = $scope.accused;
        	bizInfo.caseMemo = $scope.describeCase;
        	bizInfo.caseSituation = $scope.caseProgress;
        	bizInfo.claimAmt = $scope.claimAmt;
        	bizInfo.historyAmt = $scope.oldClaimAmt;
        	bizInfo.expectAmt = $scope.thisClaimAmt;
        	bizInfo.courtProvince = ($scope.provinceName1).province;
        	bizInfo.courtCity = ($scope.cityName1).city;
        	bizInfo.courtCountry = ($scope.districtName1).district;
        	bizInfo.courtName = $scope.obedientLawer;
        	bizInfo.closePeriod = $scope.closeDate;
        	bizInfo.totalAmt = $scope.investAllAmt;
        	bizInfo.expectCreditDate = ($scope.extendLoanDate).replace(new RegExp(/-/g),'');
        	bizInfo.bankName = $scope.bankCardName;
        	bizInfo.accountNo = $scope.bankCardNo;
        	bizInfo.branchName = $scope.branchName;
        	bizInfo.accountName = $scope.accountName;
        	
        //	bizInfo.rcScore = transData.score*1;
        //	bizInfo.rcDetail = transData.rcDetail;
        	
            
        	
    		$scope.$emit("loadingChange",true);
    		bizProxy.queryProxyBizData("vcAgentApply",headerInfo,bizInfo,
    				function(tempData){//SUCCESS
    					$scope.$emit("loadingChange",false);
					//	$timeout(function(){
					//		FIREFLY("提交成功,请等待平台审核！", "我方会在2个工作日内审核完毕","",function(){
					//			$timeout(function(){
					//				$scope.$emit("goNext","/applySurvey");
					//			},0);
					//		});
					//	},100);
					    console.log(tempData);
					    var investApplyId = JSON.parse(tempData.body);
					    var applyId  = investApplyId.applyId;
					   $scope.$emit("goNext","/applySurvey/" + applyId);
		    		},function(data){
		    			if(data != ""){//FAIL
		    				$scope.$emit("loadingChange",false);
							if(data.errorCode == ERROR_CODE.noLogin){
								FIREFLY("", data.errorMsg,"",function(){
										//调用退出登录
										transData.mobileNo = "";
		    							transData.password = "";
		    							transData.sessionId = "";
		    							transData.userType = "";
		    							resultData.loginUserData = "";
		    							resultData.loginResultData = "";
		    							$timeout(function(){
		    								$scope.$emit("goNext","/mine");
		    							},0);
								});
							}else{
								$scope.$emit("promptAlertMsg",data.errorMsg);
								$scope.$emit("showCollect",true);
								$timeout(function(){
									$scope.$emit("showCollect",false);
								},transData.promptTime);
							}
		    			}else{
		    				$scope.$emit("loadingChange",false);
							$scope.$emit("promptAlertMsg","连接超时");
							$scope.$emit("showCollect",true);
							$timeout(function(){
								$scope.$emit("showCollect",false);
							},transData.promptTime);
		    			}
		    		});
        }
    	
    	
    	
    	//整理省份数据
    	function collateData() {
    		cityListTemp = new Array();
    		districtTemp = new Array();
			for(var key in cityData.addressDetail){
				var tempData = {"province":key};
				provinceList.push(tempData);
				collateCity(key,cityData.addressDetail[key]);
			}
    	}
    	//整理城市数据
		function collateCity(data1,data2) {
			for(var key in data2){
				var tempList = {'province':data1,'city': key};
				cityListTemp.push(tempList);
				districtCity(key,data2[key]);
			}
		}
		//整理县区数据
		function districtCity(data1,data2){
			var tempList = {'city': data1,'district': data2};
			districtTemp.push(tempList);
		}
		
    	
    	
        $scope.$apply();
    }];
});